package com.jwt_demo.jwt_demo.interceptor;

import com.jwt_demo.jwt_demo.util.JwtUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class TokenInterceptor implements HandlerInterceptor {

    //拦截器实现token验证
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 如果是登录放行
        String requestURI = request.getRequestURI();
        if (requestURI.contains("login")) {
            return true;
        }
        //如果是其他接口验证token
        String token = request.getHeader("token");
        boolean b = JwtUtil.validateToken(token);
        return b;
    }
}
